home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / apps / 42 / wind11.prf < prev    next >
Text File  |  1986-07-17  |  22KB  |  407 lines

  1. .!****************************************************************************
  2. .! 
  3. .! ANTIC PUBLISHING INC., COPYRIGHT 1985.  REPRINTED BY PERMISSION.
  4. .!
  5. .! ** Professional GEM ** by Tim Oren
  6. .!
  7. .! Proff File by ST enthusiasts at
  8. .! Case Western Reserve University
  9. .! Cleveland, Ohio
  10. .! uucp : decvax!cwruecmp!bammi
  11. .! csnet: bammi@case
  12. .! arpa : bammi%case@csnet-relay
  13. .! compuserve: 71515,155
  14. .!
  15. .!****************************************************************************
  16. .!
  17. .!
  18. .!****************************************************************************
  19. .!
  20. .!            Begin Part 11
  21. .!
  22. .!****************************************************************************
  23. .!
  24. .PART XI GEM Hooks and Hacks
  25. .PP
  26. Welcome  to  the  eleventh episode of ST PRO  GEM,  which  is
  27. devoted to exploring some of the little documented,  but powerful,
  28. features  of GEM.   Like the authors of most complex systems,  the
  29. GEM  programmers left behind a set of "hooks",  powerful  features
  30. which would aid them in enhancing the system later.  I am going to
  31. lay  out  a number of these methods which have served me  well  in
  32. making  creative use of the AES.   You will find that most of them
  33. concern  the object and form libraries,  since I was most involved
  34. in those parts of GEM.  There are probably many more useful tricks
  35. waiting  to be found in other parts of GEM,  so if you happen onto
  36. one, please let me know in the Feedback!  Before you begin,be sure
  37. to  pick up the download for this column:  GMCL11.C in DL3 of PCS-
  38. 57.
  39. .SH POWERFUL   OBJECTS
  40. The  first  four tricks all involve  augmenting  standard  AES
  41. objects.   This is a powerful technique for two  reasons.   First,
  42. you  can  take  advantage  of the  regular  AES  object  and  form
  43. libraries  to draw and handle most of your objects,  so that  your
  44. program need only process the exceptions.  Second, you can use the
  45. RCS  to  copy  the  special  objects  into  multiple  dialogs   or
  46. resources.   These  four tricks are Extended Object  Types,  User-
  47. defined Objects,  TOUCHEXIT,  and INDIRECT.  Let's look at each of
  48. them in turn.
  49. .SH EXTENDED OBJECT TYPES
  50. If you look at the AES Object Library documentation,  you will
  51. notice that the values for the OB_TYPE field in an object are  all
  52. 32 or less.   This means that a number of bits are unused in  this
  53. field.   In fact,  the AES completely ignores the top byte of  the
  54. OB_TYPE field.   In addition, the RCS ignores the top byte, but it
  55. also  preserves  its value when an object  is  read,  written,  or
  56. copied.
  57. .PP
  58. This  gives  you one byte per object to use as you  see  fit.
  59. Since  the  processing of an object or dialog is (so far)  in  the
  60. hands of the AES,  the best uses of Extended Types are in flagging
  61. methods for initializing an object or handling its value when  the
  62. dialog  completes.
  63. .PP
  64. For  example,  you  might  have  several  dialogs  containing
  65. editable numeric fields.   The Extended Type of each numeric field
  66. could be set to the index of the corresponding value in an  array.
  67. Then your application's dialog initialization code could scan  the
  68. object  tree  for  such objects,  pick up  the  appropriate  value
  69. from the array and convert it to ASCII,  storing the result in the
  70. resource's  string  area.   When the dialog was finished,  another
  71. pass could be made to reconvert the ASCII to binary and store away
  72. the results in the array.   (Note that the map_tree() utility from
  73. column #5 will scan an entire resource tree.)
  74. .PP
  75. Another  application  is  to assign  uniform  codes  to  exit
  76. buttons  in  dialogs.   If you give every "OK" button an  Extended
  77. Type  of one,  and every "Cancel" button an Extended Type of  two,
  78. then  you  needn't  worry about naming every  exit  object.   Just
  79. examine  the Extended Type of the object returned by form_do,  and
  80. proceed accordingly.
  81. .PP
  82. The catch, of course, is that you have to find a way to enter
  83. the  Extended  Type code in the first place.   Version 1.0 of  the
  84. RCS, as shipped with the Atari developer's kit, makes no provision
  85. for this.  So you have your choice of two methods for creating the
  86. first  object with each Extended Type code.
  87. .PP
  88. First,  you can dump out a C source of a resource, insert the
  89. new type code by hand,  and regenerate the resource with STCREATE.
  90. Alternately,  you could carefully modify the binary resource using
  91. SID.   You will probably want to reread the AES object manual,  ST
  92. PRO GEM #4 and #5,  and use the C source as a guide when doing so.
  93. In both cases, you should make things easy on yourself by creating
  94. a  one  dialog resource with only a single object other  than  the
  95. root.   Version  2.0  of  the RCS will let you directly  enter  an
  96. Extended Type, but it has not yet been released for the ST by DRI.
  97. .PP
  98. Once  you have created a prototype extended object by  either
  99. method, you can use the RCS to propogate it.  The safest way is to
  100. use the MERGE option to add the modified tree to the resource  you
  101. are building.  Then copy the prototype object via the clipboard to
  102. your dialog(s), deleting the extra tree when you are done.  If you
  103. are  using several different extended objects,  you can use  MERGE
  104. and clipboard copies to get them all into one tree which will then
  105. become your own object library.
  106. .PP
  107. The  second way of using RCS is easier,  but more  dangerous.
  108. If you want to try the following technique,  BACK UP YOUR RCS DISK
  109. FIRST!   Put  simply,  the RCS does not care what is in its dialog
  110. partbox. It will make copies of anything that it finds there! This
  111. gives you the option of using the RCS on ITS OWN RESOURCE in order
  112. to add your customized objects to the partbox.
  113. .PP
  114. To do this, open RCS.RSC from the RCS.  Since there is no DEF
  115. file,  you will get a collection of question mark icons.   Use the
  116. NAME option to make TREE5 into a DIALOG.    Open it,  and you will
  117. see the dialog partbox.
  118. .PP
  119. Now you can use the MERGE technique described above to insert
  120. your  customized  objects.   Then SAVE the modified resource,  and
  121. rerun the RCS.  Your new objects should now appear in the partbox.
  122. If  you added several,  you may have to stretch the partbox to see
  123. them  all.   You can now make copies of the new objects just  like
  124. any other part.  (Note: DO NOT modify the alert or menu partboxes,
  125. you will probably crash the RCS.)
  126. .SH USER-DEFINED  OBJECTS
  127. The one type of object which was not discussed in the  earlier
  128. articles  on  AES objects was G_USERDEF,  the  programmer  defined
  129. object.   This  is  the  hook  for  creating  objects  with  other
  130. appearances  beyond those provided by the standard  AES.   By  the
  131. way, you should note that the G_PROGDEF and APPLBLK mnemonics used
  132. in  the  AES documents are incorrect;  the actual  names  as  used
  133. defined OBDEFS.H are G_USERDEF and USERBLK.
  134. .PP
  135. The  RCS does not support the creation of G_USERDEF  objects,
  136. since  it  has  no idea how they will be drawn  by  your  program.
  137. Instead,  you  must insert a dummy object into your resource where
  138. you  want  the  G_USERDEF  to appear,  and  patch  it  after  your
  139. application performs its resource load.
  140. .PP
  141. You   must  replace  the  object's  existing   OB_TYPE   with
  142. G_USERDEF,  though you may still use the upper byte as an Extended
  143. Type.   You  must  also  change the OB_SPEC field to be  a  32-bit
  144. pointer  to  a USERBLK structure.   An USERBLK is simply two  LONG
  145. (32-bit)  fields.   The  first is the address of the drawing  code
  146. associated  with  the  user  defined object.   The  second  is  an
  147. arbitrary 32-bit value assigned to the object by your application.
  148. .PP
  149. You can designate objects for conversion to G_USERDEFs in the
  150. normal fashion by assigning them names which are referenced one by
  151. one in your initialization code.   You can also combine two tricks
  152. by using the Extended Type field as a designator for objects to be
  153. converted to G_USERDEF.  Each tree can then be scanned for objects
  154. to  be converted.   There is a short code segment in the  download
  155. which demonstrates this technique.
  156. .PP
  157. My  usual  convention  is to define new  drawing  objects  as
  158. variants  of  existing objects,  using the Extended Type field  to
  159. designate the particular variation.   Thus an Extended Type of one
  160. might designate a G_BUTTON with rounded corners,  while a value of
  161. two  could  flag  a G_STRING of boldface text.   When  using  this
  162. technique,  the  RCS can be used to build a rough facsimile of the
  163. dialog  by inserting the basic object type as  placeholders.   The
  164. existing  OB_SPEC  information can then be copied  to  the  second
  165. position in the USERBLK when the object is initialized.
  166. .PP
  167. One final note before moving on:  There is no reason that the
  168. USERBLK  cannot be extended beyond two fields.   You might want to
  169. add  extra words to store more information related to drawing  the
  170. object, such as its original type.
  171. .PP
  172. The  AES will call your drawing code whenever  the  G_USERDEF
  173. needs  to be drawn.   This occurs when you make an objc_draw  call
  174. for its tree,  or when an objc_change occurs for that object.   If
  175. your user-defined object is in a menu drop-drop, then your drawing
  176. code will be called any time the user exposes that menu.
  177. .PP
  178. Before  getting  into the details of the AES  to  application
  179. calling  sequence,  some  warnings are in order.   First,  remember
  180. that your drawing code will execute in the AES' context, using its
  181. stack.   Therefore,  be careful not to overuse the stack with deep
  182. recursion, long parameter lists, or large dynamic arrays.  Second,
  183. the  AES  is NOT re-entrant,  so you may not make ANY calls to  it
  184. from within a G_USERDEF procedure.   You may,  of course, call the
  185. VDI.   Finally,  realize  that drawing code associated with a menu
  186. object may be called by the AES at any time.   Exercise great care
  187. in  sharing  data  space between such code and  the  rest  of  the
  188. application!
  189. .PP
  190. When your drawing code is called by the AES, the stack is set
  191. up  as if a normal procedure call had occured.   There will be one
  192. parameter  on the stack:  a 32-bit pointer to a PARMBLK structure.
  193. This structure lies in the AES' data space, so do not write beyond
  194. its end!
  195. .PP
  196. The  PARMBLK contains 15 words.   The first two are the  long
  197. address of the object tree being drawn,  and the third word is the
  198. number of the G_USERDEF object.   You may need these values if the
  199. same  drawing  code is used for more than one object  at  a  time.
  200. Words  four  and five contain the previous  and  current  OB_STATE
  201. values of the object.  If these values are different, your drawing
  202. code  is  being  called in response  to  an  objc_change  request.
  203. Otherwise, the active AES call is an objc_draw.
  204. .PP
  205. Words six through nine contain the object's rectangle on  the
  206. screen.   Remember  that  you cannot call objc_offset  within  the
  207. drawing code,  so you will need these values!  The next four words
  208. contain the clipping rectangle specified in the active objc_change
  209. or objc_draw call.   You should set the VDI clip rectangle to this
  210. value before doing any output to the screen.
  211. .PP
  212. The last two words in the PARMBLK contain a copy of the extra
  213. 32-bit parameter from the object's USERBLK.   If you have followed
  214. the  method of copying an OB_SPEC into this location,  these words
  215. will be your pointer to a string, or BITBLK, or whatever.
  216. .PP
  217. When  your drawing routine is done,  it should return a  zero
  218. value  to  the AES.   This is a "magic" value;  anything else will
  219. stop the drawing operation.
  220. .PP
  221. The download contains a sample drawing routine which  defines
  222. one extended drawing object,  a rounded rectangle button.  You can
  223. use  this procedure as a starting point for your own User  Defined
  224. objects.
  225. .SH PUT  ANYTHING YOU WANT ON THE DESKTOP!
  226. In  ST  PRO GEM #2,  I described the use  of  the  WF_NEWDESK
  227. wind_set  call to substitute your own object tree for  the  normal
  228. green  desktop background.   If the tree you supply contains  User
  229. Defined  objects,  you can draw whatever you want on the  desktop!
  230. Some  of the things you might try are free hand drawings  imported
  231. in  metafile  format from EasyDraw,  or whole  screen  bit  images
  232. generated by Degas.   If you do the latter, you will have to store
  233. the entire image off screen and blit parts of it to the display as
  234. requested.
  235. .PP
  236. In  any case,  remember that your desktop drawing code can be
  237. called any time that a window is moved,  so exercise the same care
  238. as with a menu drawer.   Also, be aware that making the WF_NEWDESK
  239. call  does  not force an immediate redraw of the desktop.   To  do
  240. that, do a form_dial(3) call for the entire desktop rectangle.
  241. .SH THE TOUCHEXIT FLAG
  242. The  TOUCHEXIT attribute is an alternative to  the  more  usual
  243. EXIT.   When the TOUCHEXIT bit is set in an object's OB_FLAG word,
  244. the form_do routine will exit immediately when the mouse button is
  245. pressed  with  the  cursor  over  the  object.    Your  code   can
  246. immediately take control of the mouse and display, without waiting
  247. for the release of the button.  This method is used for generating
  248. effects such as slider bars within otherwise normal dialogs.
  249. .PP
  250. The  easiest  way to code a TOUCHEXIT handler is to  place  a
  251. loop  around the form_do call.   If the object number returned  is
  252. TOUCHEXIT,  then the animation procedure is called,  followed by a
  253. resumption   of  the  form_do  (WITHOUT  recalling  form_dial   or
  254. objc_draw!).   If the object returned is a normal EXIT, the dialog
  255. is  complete and control flows to the cleanup code.
  256. .PP
  257. There is one idiosyncrasy of TOUCHEXIT which should be noted.
  258. When the AES "notices" that the mouse button has been pressed over
  259. a TOUCHEXIT,  it immediately retests the button state.   If it has
  260. already  been  released,  it  waits to see if  a  double click  is
  261. performed.  If so, the object number returned by form_do will have
  262. its  high  bit set.   If you don't care about double clicks,  your
  263. code should mask off this flag.   However, you may want to use the
  264. double click to denote some enhanced action.  For example, the GEM
  265. file selector uses a double click on one of the file name  objects
  266. to indicate a selection plus immediate exit.
  267. .SH THE INDIRECT FLAG
  268. If the INDIRECT bit is set in an object's OB_STATE  word,  the
  269. AES interprets the 32-bit OB_SPEC field as a pointer to the memory
  270. location  in which the ACTUAL OB_SPEC is to be found.   Like  User
  271. Defined objects,  this capability is not supported by the RCS,  so
  272. you  have to set up the INDIRECT bit and alter the OB_SPEC at  run
  273. time.
  274. .PP
  275. The value of INDIRECT is that you can use it to associate  an
  276. AES  object with other data or code.   The general technique is to
  277. set  up  a table with a spare 32-bit location  at  its  beginning.
  278. Then,  when initializing the application's resource,  you move the
  279. true  OB_SPEC  into  this location,  set the  INDIRECT  flag,  and
  280. replace the OB_SPEC field with a pointer to the table.  The object
  281. behaves normally during drawing and form handling. However, if you
  282. receive  its  number  from  form_do  or  objc_find,  you  have  an
  283. immediate  pointer to the associated table,  without having to  go
  284. through a lookup procedure.
  285. .PP
  286. This  technique works well in programs like the GEM  Desktop.
  287. Each  G_ICON object is set up with INDIRECT.   Its OB_SPEC goes to
  288. the  beginning of a data area defining the associated  file.   The
  289. blank  location at the beginning of file table is filled  up  with
  290. the former OB_SPEC, which points to a ICONBLK.
  291. .PP
  292. You  can also combine INDIRECT with TOUCHEXIT  when  creating
  293. objects  that  must change when they are clicked by a  user.   For
  294. instance,  a  color  selection  box  might be linked  to  a  table
  295. containing  the various OB_SPEC values through which  the  program
  296. will cycle.   Each time the user clicked on the box, the TOUCHEXIT
  297. routine would advance the table pointer,  copy the next value into
  298. the  dummy OB_SPEC location at the front of the table,  and redraw
  299. the object in its new appearance.
  300. .PP
  301. A  programmer  who wanted to follow a  truly  object-oriented
  302. "Smalltalkish" approach could use the INDIRECT method to bind  AES
  303. drawing  object to tables of associated procedures  or  "methods".
  304. For instance, one procedure could be flagged for use when the user
  305. clicked  on the object,  one when the object was dragged,  one for
  306. double-click,  and  so on.   If the table structure was capable of
  307. indicating  that  the true method was stored in another  table,  a
  308. rudimentary form of class inheritance could be obtained.
  309. .SH INSTANT CO-ROUTINES
  310. We turn to the AES event and message system for  this  trick.
  311. While   some  languages  like  Modula  2  provide  a  method   for
  312. implementing  co-routines,  there  is  no such  capability  in  C.
  313. However,  we  can  effectively  fake it by  using  the  AES  event
  314. library.
  315. .PP
  316. As  already  seen in an earlier column,  an  application  can
  317. write a message to its own event queue using the appl_write  call.
  318. Usually, this is a redraw message, but there is nothing to prevent
  319. you from using this facility to send messages from one routine  in
  320. your program to another.  To set up co-routines using this method,
  321. they  would  be  coded  as separate  procedures  called  from  the
  322. application's main event loop.
  323. .PP
  324. When  one  of the co-routines wanted to call  the  other,  it
  325. would  post  a message containing the request and  any  associated
  326. parameters into the application's queue and then return.  The main
  327. loop  would find the message and make the appropriate call to  the
  328. second co-routine.  It it was necessary to then re-enter the first
  329. co-routine  at  the  calling point,  the  original  message  could
  330. contain an imbedded reply message to be sent back when the request
  331. was  complete.   A  simple switch structure could then be used  to
  332. resume at the appropriate point.
  333. .PP
  334. There  are two potential problems in using this method.   The
  335. first is the limited capacity of the application event queue.  The
  336. queue  contains  eight entries.    While the AES  economizes  this
  337. space  by  merging redraws and multiple events,  it  cannot  merge
  338. messages.   Because  of this limit,  you must be extremely careful
  339. when  one  message received has the potential to generate  two  or
  340. more  messages sent.   Unless this situation is carefully managed,
  341. you  can get a sort of "cancer" which will overflow the queue  and
  342. probably crash your application.
  343. .PP
  344. The second danger involves race conditions.   Message sent by
  345. the  application  are posted to the end of the  queue.   If  other
  346. events  have occurred,  such as mouse clicks or keyboard  presses,
  347. they will be seen and processed ahead of the application generated
  348. message.   This  implies  that you cannot use this method  if  the
  349. program must complete its action before a new user generated event
  350. can be processed.
  351. .SH THAT'S  ALL  FOR NOW
  352. Hopefully these hints will keep you profitably occupied for  a
  353. while.   ST PRO GEM number twelve will return to the topic of user
  354. interfaces.   Reaction  to the first article on this  subject  was
  355. mostly  positive,  but a lot of folks wanted to see real  code  as
  356. well.   In response to your feedback,  there will also be code for
  357. implemented  your  own "mouse sensitive" objects  which  highlight
  358. when the cursor touches them.   This will be presented as part  of
  359. an alternate form manager.
  360. .SH UPDATE: ATARI ST
  361. I  have  recently gotten more  information  on  some  topics
  362. mentioned in earlier articles.   These notes will bring you up  to
  363. date:
  364. .PP
  365. A number of developers reported that they were unable to get
  366. the  self-redraw  technique described in ST PRO GEM  #2  to  work.
  367. This is usually due to a bug in the appl_init binding in Alcyon C.
  368. The  value  returned from the function,  which would  normally  be
  369. assigned  to gl_apid,  is coming back as garbage.   To work around
  370. the problem,  declare EXTERN WORD gl_apid;  in your program and DO
  371. NOT  assign the value from appl_init.   The binding WILL make  the
  372. assignment.  A tip of the hat to Russ Wetmore for this report.
  373. .PP
  374. The   last  column  mentioned  that  turning  off  the   clip
  375. rectangle  while drawing graphics text will speed things  up.   It
  376. turns  out that the VDI will also run at the non-clipped speed  if
  377. the  ENTIRE  string  to  be written is  within  the  current  clip
  378. rectangle.  To compound the problem, there is a one-off bug in the
  379. detection  algorithm  for  the right  edge.   That  is,  the  clip
  380. rectangle  has to be one pixel BEYOND the right edge of  the  text
  381. for the fast write to work.
  382. .PP
  383. The Feedback in ST PRO GEM #10 mentioned that there are known
  384. bugs  in  the  Alcyon C floating point  library.   In  fact,  this
  385. library  has been replaced with a new,  debugged version in recent
  386. shipments  of the Toolkit.   If you need to use floating point and
  387. have  run into this bug,  you should be able to get an update from
  388. Atari.   Also,  check  the  Atari  Developer's SIG (PCS-57) for  a
  389. possible download.
  390. .PP
  391. In addition, it turns out there is an undocumented feature in
  392. Alcyon  C  which allows you to imbed assembly code  in-line.   Try
  393. using:
  394. .FB asm()
  395. asm(".....");
  396. .FE
  397. where the dots are replaced with an assembly instruction.  You get
  398. one instruction per asm(),  one asm() per line.  Thanks to Leonard
  399. Tramiel for both of the above tidbits.
  400. .!
  401. .!
  402. .!*****************************************************************************
  403. .!*                                          *
  404. .!*                End Part 11                      *
  405. .!*                                          *
  406. .!*****************************************************************************
  407.